package agendamento;
import hibernate.dao.HibernateConnection;
import hibernate.entity.Servico;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.MaskFormatter;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JSeparator;
import javax.swing.JTable;

import skin.LookAndFeelSelector;
import utilitarios.JPanelPersonalizavel;
import utilitarios.TabelaShared;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import com.toedter.calendar.JDateChooser;

import factory.FabricaDeFrame;

import javax.swing.JScrollPane;

import java.awt.Color;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.awt.Font;
import java.awt.SystemColor;


public class FrmCadAgendamento extends JFrame {

	private JPanel contentPane;
	private JLabel lblCliente;
	private JLabel lblHorario;
	private JLabel lblData;
	private JLabel lblServico;
	private JTextField txtServico;
	private JComboBox cbListaClientes;
	private JButton btnServico;
	private JSeparator separator;
	private JButton btnAgendar;
	private JButton btnCancelar;
	private JDateChooser dtchDataAgendamento;
	private JComboBox cbHorario;
	private JLabel lblFuncionario;
	private JComboBox cbFuncionario;
	private JScrollPane scrollPaneAgendamento;
	private AgendamentoTableModel modeloAgendamento;
	private JTable tbAgendamento;
	private CtrlAgendamento ctrl = new CtrlAgendamento();
	private Servico serv = new Servico();
	
	public FrmCadAgendamento()  {
		setIconImage(Toolkit.getDefaultToolkit().getImage("src/Repositorio/icon.png"));
		setTitle("AGENDAMENTO DE HOR\u00C1RIO");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 625, 552);
		setLocationRelativeTo(null);
		contentPane = new JPanelPersonalizavel("src/Repositorio/pink_waves.png");
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		lblCliente = new JLabel("Cliente: ");
		lblCliente.setFont(new Font("Arial", Font.PLAIN, 18));
		lblCliente.setBounds(30, 169, 104, 22);
		contentPane.add(lblCliente);
		
		cbListaClientes = new JComboBox();
		cbListaClientes.setBounds(167, 164, 414, 28);
		contentPane.add(cbListaClientes);
		
		
		lblHorario = new JLabel("Hor\u00E1rio: ");
		lblHorario.setFont(new Font("SansSerif", Font.PLAIN, 18));
		lblHorario.setBounds(367, 26, 104, 24);
		contentPane.add(lblHorario);
		
		cbHorario = new JComboBox();
		cbHorario.setBounds(466, 26, 115, 28);
		contentPane.add(cbHorario);
		
		
		
		lblData = new JLabel("Data: ");
		lblData.setFont(new Font("Arial", Font.PLAIN, 18));
		lblData.setBounds(30, 32, 104, 18);
		contentPane.add(lblData);
		
		dtchDataAgendamento = new JDateChooser();
		dtchDataAgendamento.addPropertyChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent arg0) {
				if(dtchDataAgendamento.getDate() != null){
					cbHorario.removeAllItems();
					validaHorario();
					
				}
			
			}
		});
		dtchDataAgendamento.setBounds(167, 24, 153, 28);
		contentPane.add(dtchDataAgendamento);
		
		
		
		lblServico = new JLabel("Servi\u00E7o: ");
		lblServico.setFont(new Font("Arial", Font.PLAIN, 18));
		lblServico.setBounds(30, 79, 104, 18);
		contentPane.add(lblServico);
		
		txtServico = new JTextField();
		txtServico.setBounds(167, 72, 188, 28);
		contentPane.add(txtServico);
		txtServico.setColumns(10);
		
		btnServico = new JButton("Servi\u00E7os");
		btnServico.setBounds(492, 77, 89, 28);
		contentPane.add(btnServico);
		btnServico.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				FrmEscolheServico escolheServico = new FrmEscolheServico();
				escolheServico.setVisible(true);
				escolheServico.setRetorno(new IRetornaServico() {
					
					@Override
					public void retorna(Servico servico) {
						
						serv = servico;
						txtServico.setText(serv.getNome());
					}
				});
				
			}
		});
		
		
		
		
		separator = new JSeparator();
		separator.setBounds(10, 215, 581, 2);
		contentPane.add(separator);
		
		btnAgendar = new JButton("Agendar");
		btnAgendar.setBounds(285, 454, 140, 46);
		contentPane.add(btnAgendar);
		btnAgendar.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				add();
				clean();
			}
				
		});
		
		btnCancelar = new JButton("Cancelar");
		btnCancelar.setBounds(451, 454, 140, 46);
		contentPane.add(btnCancelar);
		btnCancelar.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);
			}
		});
		
		tbAgendamento = new JTable();
		
		
	
		scrollPaneAgendamento = new JScrollPane();
		scrollPaneAgendamento.setBounds(10, 232, 593, 210);
		scrollPaneAgendamento.setViewportView(tbAgendamento);
		contentPane.add(scrollPaneAgendamento);
		
		lblFuncionario = new JLabel("Funcion\u00E1rio:");
		lblFuncionario.setFont(new Font("Arial", Font.PLAIN, 18));
		lblFuncionario.setBounds(30, 124, 104, 18);
		contentPane.add(lblFuncionario);
		
		cbFuncionario = new JComboBox();
		cbFuncionario.setBounds(167, 119, 414, 28);
		contentPane.add(cbFuncionario);
		
		this.addWindowListener(new WindowAdapter() {
			public void windowOpened(WindowEvent e) {
				load();
			}
		});
		
	}
	
	
	public void load(){
		
		ctrl.preencheCbCliente(cbListaClientes);
		ctrl.preencheCbFuncionario(cbFuncionario);
		
	}
	
	public void validaHorario(){
		cbHorario.removeAll();
		ctrl.preencheCbHorario(cbHorario, dtchDataAgendamento);
		
	}
	
	public void add(){
		ctrl.adiciona(dtchDataAgendamento, cbFuncionario, cbListaClientes,cbHorario, serv);
		try {
			new TabelaShared().getTbAgendamentoPrincipal().setModel(new AgendamentoTableModel(new SimpleDateFormat("dd/MM/yyyy").format(new Date())));
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
	
	public void clean(){
		dtchDataAgendamento.setDate(null);
		cbHorario.removeAllItems();
		serv = null;
		txtServico.setText("");
		
		
	}
}
